package com.yiqi.liebang.feature.mine.view;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.T;
import com.yiqi.liebang.R;
import com.yiqi.liebang.common.widget.dialog.BounceTopEnter;
import com.yiqi.liebang.common.widget.dialog.NormalDialog;
import com.yiqi.liebang.common.widget.dialog.OnBtnClickL;
import com.yiqi.liebang.common.widget.dialog.SlideBottomExit;
import com.yiqi.liebang.entity.bo.AddTopicBo;
import com.yiqi.liebang.entity.bo.UserCenterBo;
import com.yiqi.liebang.feature.mine.contract.TopicContract;
import com.yiqi.liebang.feature.mine.dependencies.topic.DaggerTopicComponent;
import com.yiqi.liebang.feature.mine.dependencies.topic.TopicPresenterModule;
import com.yiqi.liebang.framework.api.UserApi;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class AddTopicActivity extends BaseActivity implements Toolbar.OnMenuItemClickListener, TopicContract.View {
    @Inject
    TopicContract.Presenter mPresenter;

    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.edt_topic_name)
    EditText mEdtTopicName;
    @BindView(R.id.rgp_topic_type)
    RadioGroup mRgpTopicType;
    @BindView(R.id.rgp_topic_time)
    RadioGroup mRgpTopicTime;
    @BindView(R.id.edt_topic_time)
    EditText mEdtTopicTime;
    @BindView(R.id.tv_topic_prize_preferential)
    TextView mTvTopicPrizePreferential;
    @BindView(R.id.tv_topic_prize)
    TextView mTvTopicPrize;
    @BindView(R.id.edt_topic_introduce)
    EditText mEdtTopicIntroduce;
    @BindView(R.id.edt_topic_info)
    EditText mEdtTopicInfo;
    @BindView(R.id.btn_topic_delete)
    TextView mBtnTopicDelete;

    private double mPrizePreferential;//优惠价格
    private double mPrizeOriginal;//原价
    private String mTimes;//时常
    private int mType = 0;//话题类型

    private boolean isAddTopic = false;

    private UserCenterBo.TopicBean mEditTopicBean;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_add_topic;
    }

    @Override
    protected void initInjector() {
        DaggerTopicComponent.builder().topicPresenterModule(new TopicPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initData() {
        isAddTopic = this.getIntent().getBooleanExtra("is_add", false);
        if (!isAddTopic) {
            mToolbar.inflateMenu(R.menu.menu_save);
            mBtnTopicDelete.setVisibility(View.VISIBLE);
            mToolbar.setOnMenuItemClickListener(this);
            initToolBar(mToolbar, "修改话题", true, true);
            mEditTopicBean = (UserCenterBo.TopicBean) this.getIntent().getSerializableExtra("topic");
            if (mEditTopicBean != null) {
                mEdtTopicName.setText(mEditTopicBean.getTopicName());
                mRgpTopicType.check(mEditTopicBean.getServiceType() == 0 ? R.id.rgb_topic_offline : R.id.rgb_topic_online);
                mType = mEditTopicBean.getServiceType();
                mEdtTopicIntroduce.setText(mEditTopicBean.getServiceIn());
                mEdtTopicInfo.setText(mEditTopicBean.getRemarks());

                mPrizePreferential = mEditTopicBean.getTopicPrice();//优惠价格
                mPrizeOriginal = mEditTopicBean.getOriginalPrice();//原价
                mTvTopicPrize.setText(mPrizeOriginal + "元/次");
                mTvTopicPrizePreferential.setText(mPrizePreferential + "元/次");


                mTimes = mEditTopicBean.getServiceTime();
                if (mEditTopicBean.getServiceTime().equals("1")) {
                    mRgpTopicTime.check(R.id.rgb_topic_time_1);
                    mEdtTopicTime.setVisibility(View.GONE);
                } else if (mEditTopicBean.getServiceTime().equals("1.5")) {
                    mRgpTopicTime.check(R.id.rgb_topic_time_1_5);
                    mEdtTopicTime.setVisibility(View.GONE);
                } else if (mEditTopicBean.getServiceTime().equals("2")) {
                    mRgpTopicTime.check(R.id.rgb_topic_time_2);
                    mEdtTopicTime.setVisibility(View.GONE);
                } else {
                    mRgpTopicTime.check(R.id.rgb_topic_time_custom);
                    mEdtTopicTime.setText(mEditTopicBean.getServiceTime());
                    mEdtTopicTime.setVisibility(View.VISIBLE);
                }

            }
        } else {
            mBtnTopicDelete.setVisibility(View.GONE);
            mToolbar.inflateMenu(R.menu.menu_add);
            mToolbar.setOnMenuItemClickListener(this);
            initToolBar(mToolbar, "添加话题", true, true);
        }
    }

    @Override
    protected void initView() {
        mTvTopicPrize.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);


    }

    @Override
    protected void initEvent() {
        mRgpTopicType.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rgb_topic_offline:
                        mType = 0;
                        break;
                    case R.id.rgb_topic_online:
                        mType = 1;
                        break;
                }
            }
        });

        mRgpTopicTime.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rgb_topic_time_1:
                        mEdtTopicTime.setVisibility(View.GONE);
                        mEdtTopicTime.setText("");
                        mTimes = "1";
                        break;
                    case R.id.rgb_topic_time_1_5:
                        mEdtTopicTime.setVisibility(View.GONE);
                        mEdtTopicTime.setText("");
                        mTimes = "1.5";
                        break;
                    case R.id.rgb_topic_time_2:
                        mEdtTopicTime.setVisibility(View.GONE);
                        mEdtTopicTime.setText("");
                        mTimes = "2";
                        break;
                    case R.id.rgb_topic_time_custom:
                        mEdtTopicTime.setVisibility(View.VISIBLE);
                        break;
                }
            }
        });
    }

    @OnClick(R.id.btn_add_topic_prize)
    public void onViewClicked() {
        Intent intent2 = new Intent();
        intent2.putExtra("mPrizeOriginal", mPrizeOriginal);
        intent2.putExtra("mPrizePreferential", mPrizePreferential);
        intent2.setClass(this, AddTopicPrizeActivity.class);
        startActivityForResult(intent2, 2);
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        if (item.getItemId() == R.id.action_add) {
            mPresenter.userAddTopic(mEdtTopicName.getText().toString().trim(),
                    mType + "",
                    TextUtils.isEmpty(mEdtTopicTime.getText().toString()) ? mTimes : mEdtTopicTime.getText().toString(),
                    mPrizePreferential,
                    mPrizeOriginal,
                    mEdtTopicIntroduce.getText().toString(),
                    mEdtTopicInfo.getText().toString()
            );

        } else if (item.getItemId() == R.id.action_next) {
            mPresenter.userEditTopic(mEditTopicBean.getId(), mEdtTopicName.getText().toString().trim(),
                    mType + "",
                    TextUtils.isEmpty(mEdtTopicTime.getText().toString()) ? mTimes : mEdtTopicTime.getText().toString(),
                    mPrizePreferential,
                    mPrizeOriginal,
                    mEdtTopicIntroduce.getText().toString(),
                    mEdtTopicInfo.getText().toString()
            );
        }
        return false;
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null) return;
        switch (requestCode) {
            case 2:
                mPrizeOriginal = data.getDoubleExtra("original_prize", 0.00);
                mPrizePreferential = data.getDoubleExtra("preferential_prize", 0.00);
                mTvTopicPrize.setText(mPrizeOriginal + "元/次");
                mTvTopicPrizePreferential.setText(mPrizePreferential + "元/次");
                break;

        }
    }

    @Override
    public void showAddTopicSuccess() {
        finish();
    }

    @Override
    public void showError(String error) {
        showMsg(error);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.btn_topic_delete)
    public void onViewdELETEClicked() {
        NormalDialog normalDialog = new NormalDialog(this);
        normalDialog.content("是否删除此条话题?")//
                .showAnim(new BounceTopEnter())//
                .dismissAnim(new SlideBottomExit())
                .show();
        normalDialog.setOnBtnClickL(
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        try {
                            if (normalDialog != null) {
                                normalDialog.dismiss();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                },
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        AM.api().createApiService(UserApi.class)
                                .delTopic(new AddTopicBo(mEditTopicBean.getId()))
                                .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer())
                                .subscribe(new Observer<String>() {
                                    @Override
                                    public void onSubscribe(Disposable d) {

                                    }

                                    @Override
                                    public void onNext(String s) {
                                        Intent intent = new Intent();
                                        setResult(2, intent);
                                        finish(); //结束当前的activity的生命周期
                                    }

                                    @Override
                                    public void onError(Throwable e) {
                                        T.showShort(e.getMessage());
                                    }

                                    @Override
                                    public void onComplete() {
                                        if (normalDialog != null) {
                                            normalDialog.dismiss();
                                        }
                                    }
                                });
                    }
                });

    }


}